from django.db import models
from user.models import UserInfo
from goods.models import GoodsShelf
from address.models import Address
from utils.models import BaseModel


# Create your models here.

class Order(BaseModel):
    pay_choice = (
        (0, '未支付'),
        (1, '支付成功'),
        (2, '支付失败'),
    )
    now_pay_type = (
        (0, '无'),
        (1, '支付宝支付'),
        (2, '微信支付'),
    )
    order_choice = (
        (0, '待支付'),
        (1, '已支付'),
        (2, '待发货'),
        (3, '待收货'),
        (4, '已收货'),
    )
    deleted_choice = (
        (0, '未删除'),
        (1, '已删除'),
    )
    subject = models.CharField(max_length=150, verbose_name='订单标题')
    order_form = models.CharField(max_length=188, null=True, blank=True, verbose_name='订单号', unique=True)
    user = models.ForeignKey(UserInfo, verbose_name='用户名', on_delete=models.CASCADE, db_constraint=False)
    pay_status = models.SmallIntegerField(choices=pay_choice, default=0, verbose_name='支付状态')
    order_total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="订单总价", default=0)
    pay_type = models.SmallIntegerField(choices=now_pay_type, default=0, verbose_name='支付类型')
    buy_num = models.IntegerField(verbose_name='购买数量', default=0)
    order_status = models.SmallIntegerField(choices=order_choice, default=0, verbose_name='订单状态')
    trade_no = models.CharField(max_length=64, null=True, blank=True, verbose_name="流水号", unique=True)
    deleted_type = models.SmallIntegerField(choices=deleted_choice, default=0, verbose_name='是否删除该订单')
    url = models.CharField(max_length=1000,verbose_name='支付链接',null=True, blank=True,)
    pay_time = models.DateTimeField(auto_now_add=True, verbose_name='支付时间')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    update_time = models.DateTimeField(auto_now=True, verbose_name='最近修改时间')
    addr = models.ForeignKey(Address, related_name='order_addr', on_delete=models.CASCADE, db_constraint=False,
                             verbose_name='地址', null=True)

    class Meta:
        db_table = 'shop_order'
        verbose_name = '订单记录'
        verbose_name_plural = "订单记录"


    def __str__(self):
        return self.subject

    def order_status_name(self):
        return self.get_order_status_display()

    def pay_status_name(self):
        return self.get_pay_status_display()

    def pay_type_name(self):
        return self.get_pay_type_display()

    @property
    def commodity_list(self):
        data_list = []
        for item in self.order_goods.all():
            data_list.append({
                "id": item.id,
                "goods_name": item.goods.goods_name,
                "original_price": item.goods.original_price,
                "goods_default": item.goods.goods_detail,
                "goods_img": 'http://127.0.0.1:8000/media/' + str(item.goods.goods_img),
                "real_price": item.goods.selling_price,
            })
        return data_list

    @property
    def addr_info(self):
        return {
            "addr_name": self.addr.name,
            "addr_province_name": self.addr.province_name,
            "addr_city_name": self.addr.city_name,
            "addr_region_name": self.addr.region_name,
            "addr_detail_address": self.addr.detail_address,
            "addr_tel": self.addr.tel,
            "ship_address": self.addr.ship_address,
        }

    def user_name(self):
        return self.user.username


class OrderDetail(models.Model):
    addr = models.ForeignKey(Address, related_name='addr_order', on_delete=models.CASCADE, db_constraint=False,
                             null=True, blank=True, verbose_name='地址')
    order = models.ForeignKey(Order, related_name='order_goods', on_delete=models.CASCADE, db_constraint=False,
                              verbose_name='订单')
    goods = models.ForeignKey(GoodsShelf, related_name='goods_order', on_delete=models.CASCADE, db_constraint=False,
                              verbose_name='商品')
    price = models.DecimalField(max_digits=8, decimal_places=2, verbose_name='商品原价')
    real_price = models.DecimalField(max_digits=8, decimal_places=2, verbose_name='商品实际支付价格')
    buy_num = models.IntegerField(verbose_name='购买数量', null=True)

    class Meta:
        db_table = 'sagacity_order_detail'
        verbose_name = '订单详情'
        verbose_name_plural = '订单详情'

    def addr_info(self):
        return {"addr_name": self.addr.name,
                "addr_province_name": self.addr.province_name,
                "addr_city_name": self.addr.city_name,
                "addr_region_name": self.addr.region_name,
                "addr_detail_address": self.addr.detail_address,
                "addr_tel": self.addr.tel,
                }

    def __str__(self):
        try:
            return "%s的订单:%s" % (self.goods.goods_name, self.order.order_form)
        except:
            return super().__str__()



